<?php
/*   Description  of this file
*    Author king-of-war
*    Last Modified 2009-5-4
*    This class designed  for the guest book operation
*/
class guest {
private $url;
    function guest($url) {
/*
		 * init the $url as rootpath
		 *
		 */
		include_once $url . 'includes/conn.php';
		$this->set_url($url);
    }


    /*
     * guest book add && update
     *
     * $fun 1 means add
     *
     * 2 means  repeta
     */
     function guest_add($id,$name,$comments,$email,$replay,$verify,$fun){
	    $conn = new conn();



			if ($fun) {
				/*
				 * add guest_book sql
				 *
				 */
				$sql = sprintf("INSERT INTO guest_book(id,name,comments,date,email,verify)VALUES(null,'%s','%s',now(),'%s','%d')", $name, $comments, $email, $verify);

			}
			else {
				/*
				* replay guest_book sql
				*/
				$sql = sprintf("update guest_book set replay='%s',re_date=now() where id='%s'", $replay,$id);
			}

			$conn->set_sql($sql);
		

			$line = $conn->get_line();

			return $line;

     }

     function guest_verify($id,$verify){

     	$line=0;
     	if(is_array($id)){
     		for($i=0;$i<count($id);$i++){
     			 	$conn=new conn();
     			$sql=sprintf(" update guest_book set verify='%d' where id='%d'",$verify[$i],$id[$i]);
    				$conn->set_sql($sql);

     	$line+=$conn->get_line();
     		}
     	}


     	return $line;
     }



  /*
     * guest book del
     */
     function guest_del($ids){
	if (isset ($ids)) {
			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i < $max; $i++) {
					$conn = new conn();
					$sql = sprintf("delete from guest_book where id=%d ; ", $ids[$i]);
					$conn->set_sql($sql);
					$line1 = $conn->get_line();
					$line += $line1;
				}
			} else {
				$conn = new conn();
				$sql = sprintf("delete from guest_book where id=%d", $ids);
				$conn->set_sql($sql);
				$line = $conn->get_line();
			}
		} else {
			echo 'at least choose one for process please';
			$line = 0;
		}
		return $line;

     }

       /*
     * guest book read
     */
     function guest_read($id,$fun){
     $conn = new conn();

		$info = '';
		if (!$fun) {
			$info = ' where verify =1 ';
		}
		if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from guest_book " . $info ."ORDER BY date DESC limit %d,%d ", $a, $b);
		} else {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from guest_book where id='%d' " . $info, $id);
		}

		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$result = $conn->get_result();
			$guests=array();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {

				$guests[$i]['id'] = $row['id'];
				$guests[$i]['name'] = $row['name'];
				$guests[$i]['comments'] = $row['comments'];
				$guests[$i]['date'] = $row['date'];
				$guests[$i]['mail'] = $row['email'];
				$guests[$i]['replay'] = $row['replay'];
				$guests[$i]['re_date'] = $row['re_date'];
				$guests[$i]['verify'] = $row['verify'];
			}

		} else {
			echo 'result null';
			$news = null;
		}
		return $guests;

     }


	/*
	 * functions for use above
	 */



	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}


}

?>